'use strict';

const BaseController = require('./base');
const { createRule } = require('../validator/themeCategory')
const { success, commonErr } = require('../extend/helper');

class ThemeController extends BaseController {

    /**
     * 列表
     */
    async list() {
        const page = parseInt(this.ctx.query.page) ? parseInt(this.ctx.query.page) : 0;
        const size = parseInt(this.ctx.query.size) ? parseInt(this.ctx.query.size) : 15;
        const where = await this.where();
        const { rows, count } = await this.ctx.model.Theme.findAndCountAll({
            include: [
                {
                    model: this.ctx.model.User,
                    as: "user",
                    attributes: ['id', 'nickname']
                },
                {
                    model: this.ctx.model.ThemeCategory,
                    as: "category",
                    attributes: ['id', 'title']
                }
            ],
            where: where,
            order: [
                ['create_time', 'desc']
            ],
            offset: page * size,
            limit: size
        });
        success({
            beans: {
                rows, count, page, size

            }
        })

    }
    
    async where() {
        const userId = this.ctx.query.user_id;
        const categoryId = this.ctx.query.category_id;
        const anonymity = this.ctx.query.anonymity;
        const online =  this.ctx.query.online;
        const recommend = this.ctx.query.recommend;
        const where = {};
        if (userId) {
            where.user_id = userId;
        }
        if (categoryId) {
            where.category_id = categoryId;
        }
        if (anonymity == 'true') {
            where.anonymity = true;
        } 
        if (anonymity == 'false') {
            where.anonymity = false;
        }
        if (online == 'true') {
            where.online = true;
        } 
        if (online == 'false') {
            where.online = false;
        }
        if (recommend == 'true') {
            where.recommend = true;
        } 
        if (recommend == 'false') {
            where.recommend = false;
        }
        return where;
    }

    /**
     * 详情
     */
    async detail() {
        const id = this.ctx.params.id;
        const theme = await this.ctx.model.Theme.findByPk(id, {
            include: [
                {
                    model: this.ctx.model.User,
                    as: "user",
                    attributes: ['id', 'nickname']
                },
                {
                    model: this.ctx.model.ThemeCategory,
                    as: "category",
                    attributes: ['id', 'title']
                },
                {
                    model: this.ctx.model.ThemeImage,
                    as: 'images',
                    attributes: ['id', 'image'],
                    where: {
                        online: true
                    },
                }
            ]
        })
        success({bean: theme});
    }

    /**
     * 删除
     */
    async delete() {
        const theme = await this.getObject();
        theme.destroy();
        success();
    }

    /**
     * 切换显示按钮
     */
    async online() {
        const theme = await this.getObject();
        await this.onlineSwitch(theme);
        success();
    }

    /**
     * 切换推荐按钮
     */
     async recommend() {
        const theme = await this.getObject();
        await this.recommendSwitch(theme);
        success();
    }

    async getObject() {
        const id = this.ctx.params.id;
        const theme = await this.ctx.model.Theme.findByPk(id);
        if (!theme) {
            commonErr(`话题不存在`);
        }
        return theme;
    }

}

module.exports = ThemeController;
